package com.abc.scholarship.controller;

import cn.hutool.core.bean.BeanUtil;
import com.abc.scholarship.domain.dto.NoticeDTO;
import com.abc.scholarship.domain.entity.Notice;
import com.abc.scholarship.domain.page.PageDTO;
import com.abc.scholarship.domain.page.PageQuery;
import com.abc.scholarship.domain.vo.resp.ApiResult;
import com.abc.scholarship.domain.vo.resp.NoticeResp;
import com.abc.scholarship.exception.BusinessException;
import com.abc.scholarship.service.NoticeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-07-08  11:17
 */
@Api(tags = "公告接口")
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @ApiOperation("查询某年公告")
    @GetMapping("/IndexList/{year}")
    public ApiResult<List<NoticeResp>> list(@PathVariable Integer year){

        return ApiResult.success(noticeService.getList(year));
    }

    @ApiOperation("新增公告")
    @PostMapping("/insert")
    public ApiResult insert(@RequestBody NoticeDTO noticeDTO){
        noticeService.insert(noticeDTO);
        return ApiResult.success();
    }

    /**
     * 分页查询所有公告
     * @param page
     * @return
     */
    @GetMapping("/selectAll")
    @ApiOperation("分页查询公告")
    public ApiResult<PageDTO<Notice>> selectAll(PageQuery page) {
        Page<Notice> noticePage = noticeService.page(page.toMpPage());
        return ApiResult.success(PageDTO.of(noticePage, Notice.class));
    }

    @ApiOperation("删除公告")
    @DeleteMapping("/delete/{id}")
    public ApiResult deleteNotice(@PathVariable Integer id){
        noticeService.removeById(id);
        return ApiResult.success();
    }

    @ApiOperation("修改公告")
    @PutMapping("/update")
    public ApiResult update(@RequestBody NoticeDTO noticeDTO){
        if(noticeDTO.getTitle() == null || noticeDTO.getTitle().isEmpty()){
            throw new BusinessException("不能输入为空");
        }if(noticeDTO.getContent() == null || noticeDTO.getContent().isEmpty()){
            throw new BusinessException("不能输入为空");
        }
        noticeService.updateById(BeanUtil.copyProperties(noticeDTO, Notice.class));
        return ApiResult.success();
    }



}
